package com.dhl.fin.api.service;

import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.common.util.DateUtil;
import com.dhl.fin.api.common.util.MapUtil;
import com.dhl.fin.api.common.util.StringUtil;
import com.dhl.fin.api.domain.LineChartData;
import com.dhl.fin.api.enums.BasicAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class LineChartDataServicelmpl extends CommonService<LineChartData> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Map> getLineData(String[] codes) throws Exception {

        List<Map> result = new LinkedList<>();

        for (String code : codes) {

            List<Map> datas = select(QueryDto.builder().available().addWhere("data_code='" + code + "'")
                    .addOrder("point_date").build())
                    .stream()
                    .map(p -> MapUtil.builder()
                            .add("name", DateUtil.getTime(p.getPointDate()))
                            .add("value", p.getAmount())
                            .build())
                    .collect(Collectors.toList());

            String aspectName = BasicAspect.valueOf(code.toUpperCase()).getName();

            result.add(MapUtil.builder().add("name", aspectName).add("series", datas).build());

        }

        return result;
    }


    public List<Map> getTradeLine() {
        String sql = "select trade,date, round(sum(total_market_value),2) marketValue\n" + "from t_stock\n" + "where trade is not null and trade != ''\n" + "group by trade,date";

        List<Map<String, Object>> data = jdbcTemplate.queryForList(sql);

        String temp = "";
        List<Map> series = new LinkedList<>();
        for (Map datum : data) {
            String trade = MapUtil.getString(datum, "trade");
            Date date = MapUtil.getDate(datum, "date");
            String marketValue = MapUtil.getString(datum, "marketValue");

            if (StringUtil.isNotEmpty(temp) && !temp.equals(trade)) {
                data.add(MapUtil.builder().add("name", temp).add("series", data).build());

                temp = trade;
                series = new LinkedList<>();
            }

            series.add(MapUtil.builder().add("name", date).add("value", marketValue).build());

        }

        data.add(MapUtil.builder().add("name", temp).add("series", data).build());

        List<Map> result = new LinkedList<>();
        return result;
    }

    public void addLineData(String amount1, String amount2, String type, Date pointDate) throws Exception {
        LineChartData lineChartData = new LineChartData();
        if (type.equalsIgnoreCase("m1m2")) {
            lineChartData.setDataCode("m1");
            lineChartData.setAmount(Double.valueOf(amount1));
            lineChartData.setPointDate(pointDate);
            save(lineChartData);

            if (StringUtil.isNotEmpty(amount2)) {
                lineChartData = new LineChartData();
                lineChartData.setDataCode("m2");
                lineChartData.setAmount(Double.valueOf(amount2));
                lineChartData.setPointDate(pointDate);
                save(lineChartData);
            }
        } else {
            lineChartData.setDataCode(type);
            lineChartData.setAmount(Double.valueOf(amount1));
            lineChartData.setPointDate(pointDate);
            save(lineChartData);
        }
    }
}
